<?php
use oxide\data\Connection;
use module\content\model\Content;
use oxide\ui\html;

/**
 * Controller with User related functions for Edu Plugin 
 */
class EduUserController 
{
	/**
	 * displaying registered courses in user profile
	 * @param type $user 
	 */
	public function userProfile($user)
	{
		// initialize vars
		\oxide\application\View::load('_html');
		$metadata = $user->metadata();
		
		// get the course title and status of user registered courses
		$registered_courses = $this->_findContentValuesOfAllUserRegisteredCourses($user->user_pk);
		
		// display the profile
		$this->_renderProfile($user);
		// display the courses and their payment status
		$this->_renderRegisteredCourseAndStatus($registered_courses);
	}
	
	/**
	 * render the profile details
	 * @param array $user 
	 */
	private function _renderProfile($user)
	{
		// creating the html for display on User profile
		_html::tag('h4', 'Profile');
		_html::tag('p', 'Name: '.$user->name);
		_html::tag('br', '');
	}
	
	/**
	 * render the registered courses and status
	 * @param array $contents 
	 */
	private function _renderRegisteredCourseAndStatus($contents)
	{
		// check if user has registered for any courses
		// if no courses registered, display no courses text
		// otherwise, display the course title and status in a html list
		_html::tag('h4', 'Registered Courses');
		
		if (empty($contents)) {
			_html::tag('p', 'No courses registered');
			
		} else {
		
			_html::ul($contents);
		
		}
	}
	
	/**
	 * Setting up the user form update for registration and update info
	 * @param type $form
	 * @param type $user 
	 */
	public function userFormUpdate($form, $user)
	{
		$address = '';
		$phone = '';
		
		// check if user_pk is available
		// if true, get the user's address and phone from the metadata as assigned to vars
		if($user->user_pk) {
			$metadata = $user->metadata();
			$address = $metadata->EduPlugin_address;
			$phone = $metadata->EduPlugin_phone;
		} else {
			// if not, ie. user is not logged in/registered hence no user pk available
			// User Controller adds these fields automatically
			// and display these fields in registration form only
			$form->addControl(new html\InputControl('text', 'name', null, 'Name:'));
			$form->addControl(new html\InputControl('text', 'email', null, 'Email:'));
			$form->getInputValidator()->addRequired(array('name', 'email'));
		}
		
		// Add these common fields in both registration and user info update
		$form->addControl(new html\TextareaControl('address', $address, 'Address:'));
		$form->addControl(new html\InputControl('text', 'phone', $phone, 'Phone:'));
		$form->getInputValidator()->addRequired(array('address', 'phone'));
	}
	
	/**
	 * save/update user metadata during registration and update info
	 * @param type $user
	 * @param type $values 
	 */
	public function userMetaFormUpdate($user, $values)
	{		
		// saving of the user metadata values
		$metadata = $user->metadata();
		$metadata->EduPlugin_address = $values['address'];
		$metadata->EduPlugin_phone = $values['phone'];
		$metadata->save();
	}
	
	/**
	 * find all the course title and status of registered courses
	 * @param int $user_pk
	 * @return array 
	 */
	private function _findContentValuesOfAllUserRegisteredCourses($user_pk)
	{
		// intialize the text to display based on the status
		$course_link = "";
		$courses = array();
		
		// query for all courses that user has registered (regardless of the payment status)
		$query = new oxide\data\sql\SelectQuery('content_user_extension', Connection::sharedInstance());
		$query->column('content_user_extension.content_pk, content_user_extension.edu_status, content.title, content.permalink');
		$query->join('content', array('content.content_pk' => 'content_user_extension.content_pk'), Connection::JOIN_LEFT);
		$query->where('content_user_extension.user_pk', $user_pk, \oxide\data\Connection::OP_EQ);
		$stmt = \module\content\model\ContentUserExtension::select($query);
		$contents = $stmt->fetchAll();
		
		if (empty($contents)) { return array(); }
		
		// loop through the courses, and display course title and payment status for user registered courses
		foreach ($contents as $content) {
			// the status could be active, inactive, paid, or added
			if (!is_null($content['edu_status'])) {
			
				if ($content['edu_status'] == EduPlugin::STATUS_ACTIVE) {
					$course_link = "<a href='/content/view/{$content['permalink']}'>Go to classroom</a>";
				} else {
					$course_link = "";
				}

				$course_status_text = EduPlugin::getStringForSectionStatus($content['edu_status']);
				$course_status = ucfirst($course_status_text) ." " . $course_link;
				$courses[] = $content['title'].' | '. $course_status;
			}
		}
		
		return $courses;
	}
}
?>